{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 6]\n",
      "[1, 2, 300, 4, 5, 6]\n",
      "[1, 2, 300, 4, 5, 6]\n",
      "9\n",
      "109\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "# 传值和传地址的区别\n",
    "# 对于简单的数值，采用传值操作，即在函数内对参数的操作不影响外面的变量\n",
    "# 对于复杂变量，采用传地址操作，此时函数内的参数和外部变量是同一份内容，\n",
    "# 任何地方对此内容的更改都影响另外的变量或参数的使用\n",
    "\n",
    "def a(n):\n",
    "    n[2] = 300\n",
    "    print(n)\n",
    "    return None\n",
    "\n",
    "def b(n):\n",
    "    n += 100\n",
    "    print(n)\n",
    "    return None\n",
    "\n",
    "\n",
    "an = [1,2,3,4,5,6]\n",
    "bn = 9\n",
    "\n",
    "print(an)\n",
    "a(an)\n",
    "print(an)\n",
    "\n",
    "print(bn)\n",
    "b(bn)\n",
    "print(bn)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 关于列表的函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'i love wangxiaojing', 45, 766, (5+4j)]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l = ['a',\"i love wangxiaojing\", 45, 766, 5+4j]\n",
    "l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4]\n",
      "[1, 2, 3, 4, 100]\n"
     ]
    }
   ],
   "source": [
    "# append 插入一个内容, 在末尾追加\n",
    "a = [ i for i in range(1,5)]\n",
    "print(a)\n",
    "a.append(100)\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 100]\n",
      "[1, 2, 3, 666, 4, 100]\n"
     ]
    }
   ],
   "source": [
    "# insert: 制定位置插入\n",
    "# insert(index, data), 插入位置是index前面\n",
    "print(a)\n",
    "a.insert(3, 666)\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 666, 4, 100]\n",
      "100\n",
      "[1, 2, 3, 666, 4]\n"
     ]
    }
   ],
   "source": [
    "# 删除\n",
    "# del 删除\n",
    "# pop，从对位拿出一个元素，即把最后一个元素取出来\n",
    "print(a)\n",
    "last_ele = a.pop()\n",
    "print(last_ele)\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 666]\n",
      "140249408978184\n",
      "[1, 2, 3, 4]\n",
      "140249408978184\n"
     ]
    }
   ],
   "source": [
    "# remove:在列表中删除指定的值的元素\n",
    "# 如果被删除的值没在list中，则报错\n",
    "# 即，删除list指定值的操作应该使用try。。。excepty语句，或者先行进行判断\n",
    "# if x in list:\n",
    "#    list.remove(x)\n",
    "a.insert(4, 666)\n",
    "print(a)\n",
    "print(id(a))\n",
    "a.remove(666)\n",
    "print(a)\n",
    "print(id(a))\n",
    "\n",
    "# 输出两个id值一样，说明，remove操作是在原list直接操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4]\n",
      "140249408978184\n",
      "[]\n",
      "140249408978184\n"
     ]
    }
   ],
   "source": [
    "# clear:清空\n",
    "print(a)\n",
    "print(id(a))\n",
    "a.clear()\n",
    "print(a)\n",
    "print(id(a))\n",
    "\n",
    "# 如果不需要列表地址保持不变，则清空列表可以使用以下方式\n",
    "# a = list()\n",
    "# a = []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5]\n",
      "140249409135624\n",
      "[5, 4, 3, 2, 1]\n",
      "140249409135624\n"
     ]
    }
   ],
   "source": [
    "# reverse:翻转列表内容，原地翻转\n",
    "\n",
    "a = [ 1,2,3,4,5]\n",
    "print(a)\n",
    "print(id(a))\n",
    "\n",
    "a.reverse()\n",
    "\n",
    "print(a)\n",
    "print(id(a))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5]\n",
      "140249408367816\n",
      "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n",
      "140249408367816\n"
     ]
    }
   ],
   "source": [
    "# extend:扩展列表，两个列表，把一个直接拼接到后一个上\n",
    "\n",
    "a = [ 1,2,3,4,5]\n",
    "b = [6,7,8,9,10]\n",
    "print(a)\n",
    "print(id(a))\n",
    "\n",
    "a.extend(b)\n",
    "\n",
    "print(a)\n",
    "print(id(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 8]\n",
      "[1, 2, 3, 4, 8, 5, 6, 7, 8, 9, 10, 8, 8]\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "# count:查找列表中指定值或元素的个数\n",
    "print(a)\n",
    "a.append(8)\n",
    "a.insert(4, 8)\n",
    "print(a)\n",
    "a_len = a.count(8)\n",
    "print(a_len)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 666]\n",
      "[1, 2, 3, 777, 5, 666]\n",
      "140249408975432\n",
      "[1, 2, 3, 777, 5, 666]\n",
      "140249408975432\n",
      "********************\n",
      "[1, 2, 3, 777, 5, 666]\n",
      "140249408975432\n",
      "[1, 2, 3, 777, 5, 666]\n",
      "140249408976776\n",
      "******************************\n",
      "[1, 2, 3, 777, 5, 666]\n",
      "[1, 2, 3, 888, 5, 666]\n"
     ]
    }
   ],
   "source": [
    "# copy: 拷贝，此函数是浅拷贝，\n",
    "\n",
    "# 列表类型变量赋值示例\n",
    "a = [1,2,3,4,5,666]\n",
    "print(a)\n",
    "# list类型，简单赋值操作，是传地址\n",
    "b = a\n",
    "b[3] = 777\n",
    "print(a)\n",
    "print(id(a))\n",
    "print(b)\n",
    "print(id(b))\n",
    "\n",
    "print(\"*\" * 20)\n",
    "\n",
    "# 为了解决以上问题，list赋值需要采用copy函数\n",
    "b = a.copy()\n",
    "print(a)\n",
    "print(id(a))\n",
    "print(b)\n",
    "print(id(b))\n",
    "\n",
    "print(\"*\" * 30)\n",
    "\n",
    "\n",
    "b[3] = 888\n",
    "print(a)\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "140249408365768\n",
      "140249409236040\n",
      "140249409236232\n",
      "140249409236232\n",
      "[1, 2, 3, [10, 20, 666]]\n",
      "[1, 2, 3, [10, 20, 666]]\n"
     ]
    }
   ],
   "source": [
    "# 深拷贝跟浅拷贝的区别\n",
    "# 出现下列问题的原因是，copy‘函数是个浅拷贝函数，即只拷贝一层内容\n",
    "# 深拷贝需要使用特定工具\n",
    "a = [1,2,3, [10, 20, 30]]\n",
    "b = a.copy()\n",
    "print(id(a))\n",
    "print(id(b))\n",
    "print(id(a[3]))\n",
    "print(id(b[3]))\n",
    "a[3][2] = 666\n",
    "print(a)\n",
    "print(b)\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 元组-tuple\n",
    "- 元组可以看成是一个不可更改的list\n",
    "## 元组创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'tuple'>\n",
      "<class 'tuple'>\n",
      "(1,)\n",
      "<class 'tuple'>\n",
      "(1,)\n",
      "<class 'tuple'>\n",
      "(1, 2, 3, 4, 5)\n",
      "<class 'tuple'>\n",
      "(1, 2, 3, 4, 5)\n",
      "<class 'tuple'>\n",
      "(1, 2, 3, 4, 5)\n"
     ]
    }
   ],
   "source": [
    "# 创建空元组\n",
    "t = ()\n",
    "print(type(t))\n",
    "\n",
    "# 创建一个只有一个值的元组\n",
    "t = (1,)\n",
    "print(type(t))\n",
    "print(t)\n",
    "\n",
    "t = 1,\n",
    "print(type(t))\n",
    "print(t)\n",
    "\n",
    "# 创建多个值的元组\n",
    "t = (1,2,3,4,5)\n",
    "print(type(t))\n",
    "print(t)\n",
    "\n",
    "t =  1,2,3,4,5\n",
    "print(type(t))\n",
    "print(t)\n",
    "\n",
    "# 使用其他结构创建\n",
    "l = [1,2,3,4,5]\n",
    "t = tuple(l)\n",
    "print(type(t))\n",
    "print(t)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 元组的特性\n",
    "- 是序列表，有序\n",
    "- 元组数据值可以访问，不能修改，不能修改，不能修改\n",
    "- 元组数据可以是任意类型\n",
    "- 总之，list所有特性，除了可修改外，元组都具有\n",
    "- 也就意味着，list具有的一些操作，比如索引，分片，序列相加，相乘，成员资格操作等，一模一样"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n"
     ]
    }
   ],
   "source": [
    "# 索引操作\n",
    "t = (1,2,3,4,5)\n",
    "print(t[4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": "tuple index out of range",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-34-47f420862c37>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mt\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m12\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mIndexError\u001b[0m: tuple index out of range"
     ]
    }
   ],
   "source": [
    "# 超标错误\n",
    "print(t[12])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "140249409775272\n",
      "140249408604272\n",
      "(2, 4, 6)\n",
      "(3, 4, 5, 6)\n"
     ]
    }
   ],
   "source": [
    "t = (1,2,3,4,5,6)\n",
    "t1 = t[1::2]\n",
    "print(id(t))\n",
    "print(id(t1))\n",
    "print(t1)\n",
    "\n",
    "# 切片可以超标\n",
    "t2 = t[2:100]\n",
    "print(t2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, 3)\n",
      "140249408457464\n",
      "(1, 2, 3, 5, 6, 7)\n",
      "140249409775368\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "'tuple' object does not support item assignment",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-46-42380d3cdd39>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m     16\u001b[0m \u001b[0;31m# tuple 的不可修改，指的是内容的不可修改\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     17\u001b[0m \u001b[0;31m# 修改tuple内容会导致报错\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 18\u001b[0;31m \u001b[0mt1\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m100\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "# 序列相加\n",
    "t1 = (1,2,3)\n",
    "t2 = (5,6,7)\n",
    "\n",
    "# 传址操作\n",
    "print(t1)\n",
    "print(id(t1))\n",
    "t1 = t1 + t2\n",
    "print(t1)\n",
    "print(id(t1))\n",
    "\n",
    "# 以上操作，类似于\n",
    "t1 = (1,2,3)\n",
    "t1 = (2,3,4)\n",
    "\n",
    "# tuple 的不可修改，指的是内容的不可修改\n",
    "# 修改tuple内容会导致报错\n",
    "t1[1] = 100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, 3, 1, 2, 3, 1, 2, 3)\n"
     ]
    }
   ],
   "source": [
    "# 元组相乘\n",
    "t = (1,2,3)\n",
    "t = t * 3\n",
    "print(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "YES\n"
     ]
    }
   ],
   "source": [
    "# 成员检测\n",
    "t = (1,2,3)\n",
    "if 2 in t:\n",
    "    print(\"YES\")\n",
    "else:\n",
    "    print(\"NO\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 2 3 wangxiaojing i love "
     ]
    }
   ],
   "source": [
    "# 元组遍历，一般采用for\n",
    "# 1. 单层元组遍历\n",
    "t = (1,2,3,\"wangxiaojing\", \"i\", \"love\")\n",
    "for i in t:\n",
    "    print(i, end=\" \")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, 3)\n",
      "(2, 3, 4)\n",
      "('i', 'love', 'wangxiaojing')\n",
      "1 -- 2 -- 3\n",
      "2 -- 3 -- 4\n",
      "i -- love -- wangxiaojing\n"
     ]
    }
   ],
   "source": [
    "# 2. 双层元组的遍历\n",
    "t = ((1,2,3), (2,3,4),(\"i\", \"love\", \"wangxiaojing\"))\n",
    "\n",
    "# 对以上元组的遍历，可以如下\n",
    "# 1.\n",
    "\n",
    "for i in t:\n",
    "    print(i)\n",
    "    \n",
    "for k,m,n in t:\n",
    "    print(k,'--',m,'--',n)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
